package com.example.demo4.service;

import com.example.demo4.entity.Book;
import com.example.demo4.entity.Year_date_all;
import com.example.demo4.mapper.DateMapper;
import com.example.demo4.util.getDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: lin
 * @description:
 * @time: 2021/7/31 11:54
 */
@Service
public class DateService {
    @Autowired
    private DateMapper dateMapper;

    /**
     * 获取日期段是否有排班信息
     * @param beginDate
     * @param finishDate
     * @return
     */
    public Boolean dateIsSchema(String beginDate,String finishDate){
        //获取日期列表
        List<String> daysStr = getDate.findDaysStr(beginDate, finishDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //判断时间段是否有排班信息
        for (String s : daysStr) {
            try {
                Date date = sdf.parse(s);
                int flag = dateMapper.SchemaByDate(date);
                System.out.println(s + "该日期的排班状态为:" + flag);
                if (flag == 1){
                    return true;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    /**
     * 获取日期是否有排班信息
     * @param date
     * @return
     */
    public boolean dateIsSchema(Date date) {
        int flag = dateMapper.SchemaByDate(date);
        if (flag == 1){
            return true;
        }
        return false;
    }

    /**
     * 通过日期获得排班状态
     * @param date
     * @return
     */
    public int getStateByDate(Date date) {
        return dateMapper.getStateByDate(date);
    }
    /*
    *
    * 获取日期班次排班信息
    *
    * */
    public List showList(){
        return dateMapper.selectAll();
    }
}
